<template>
    <div>
        <div style="display: flex; justify-content: space-between;">
            <div>
                <el-button type="primary" @click="handleAdd">添加物流线路</el-button>
            </div>
            <div style="display: flex; justify-content: space-between;">
                <el-input v-model="search" placeholder="输入关键字搜索"></el-input>
                <el-button type="primary" @click="handleSelect">搜索</el-button>
                <el-button type="primary" @click="handleClear">清空</el-button> 
            </div>
        </div>

        <div class="list">
            <el-table :data="logisticsList" style="width: 100%">
                <el-table-column
                    label="线路名称"
                    width="180"
                >
                    <template slot-scope="scope">
                        <span>{{ scope.row.name }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                    label="途径"
                    width="240"
                >
                    <template slot-scope="scope">
                        <span>{{ scope.row.pathway }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                    label="地址"
                    width="240"
                >
                    <template slot-scope="scope">
                        <span>{{ scope.row.address }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                    label="电话"
                    width="200"
                >
                    <template slot-scope="scope">
                        <span>{{ scope.row.telephone }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button
                        size="mini"
                        type="primary"
                        @click="handleTop(scope.$index, scope.row)">置顶</el-button>
                        <el-button
                        size="mini"
                        type="primary"
                        @click="handleEdit(scope.$index, scope.row)">修改</el-button>
                        <el-button
                        size="mini"
                        type="danger"
                        @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <div class="block">
            <el-pagination
                layout="prev, pager, next"
                :total="totalPages"
                :page-size="10"
                :current-page="currentPage"
                @current-change="getLogisticsList"
            >
            </el-pagination>
        </div>

        <el-dialog
            title="添加物流线路"
            :visible.sync="dialogVisibleAdd"
            width="30%"
            :before-close="handleClose"
        >
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="80px" class="demo-ruleForm">
                <el-form-item label="线路名称" prop="name">
                    <el-input v-model="ruleForm.name"></el-input>
                </el-form-item>
                <el-form-item label="途径" prop="pathway">
                    <el-input v-model="ruleForm.pathway"></el-input>
                </el-form-item>
                <el-form-item label="地址" prop="address">
                    <el-input v-model="ruleForm.address"></el-input>
                </el-form-item>
                <el-form-item label="电话" prop="telephone">
                    <el-input v-model="ruleForm.telephone"></el-input>
                </el-form-item>
            </el-form>
    
            <span slot="footer" class="dialog-footer">
                <el-button @click="submitForm('ruleForm')">确定</el-button>
                <el-button @click="dialogVisibleAdd = false">取消</el-button>
            </span>
        </el-dialog>

        <el-dialog
            title="修改物流线路"
            :visible.sync="dialogVisibleEdit"
            width="30%"
            :before-close="handleClose"
        >
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="80px" class="demo-ruleForm">
                <el-form-item label="线路名称" prop="name">
                    <el-input v-model="ruleForm.name"></el-input>
                </el-form-item>
                <el-form-item label="途径" prop="pathway">
                    <el-input v-model="ruleForm.pathway"></el-input>
                </el-form-item>
                <el-form-item label="地址" prop="address">
                    <el-input v-model="ruleForm.address"></el-input>
                </el-form-item>
                <el-form-item label="电话" prop="telephone">
                    <el-input v-model="ruleForm.telephone"></el-input>
                </el-form-item>
            </el-form>
    
            <span slot="footer" class="dialog-footer">
                <el-button @click="submitEditForm('ruleForm')">保存</el-button>
                <el-button @click="dialogVisibleEdit = false">取消</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    export default{
        data() {
            return {
                search:'',
                totalPages:0,
                currentPage:1,
                logisticsList:[],
                dialogVisibleAdd: false,
                dialogVisibleEdit: false,
                ruleForm: {
                    name: '',
                    pathway: '',
                    address: '',
                    telephone: ''
                },
                rules: {
                    name: [
                        { required: true, message: '请输入线路名称！', trigger: 'blur' },
                    ],
                    pathway: [
                        { required: true, message: '请输入途径！', trigger: 'blur' },
                    ],
                    address: [
                        { required: true, message: '请输入地址！', trigger: 'blur' },
                    ],
                    telephone: [
                        { required: true, message: '请输入电话号码！', trigger: 'blur' },
                        { pattern: /^\d{11}$/, message: '请输入正确的号码格式!', trigger: 'blur' }
                    ]
                }
            }
        },
        methods: {
            async handleSelect() {
                let res = await this.$axios.get('/search',{params: {search:this.search},headers: {Authorization:localStorage['token']}})

                this.logisticsList = res.data.data
                this.totalPages = res.data.totalPages
            },
            handleClear() {
                this.getLogisticsList()
            },
            handleAdd() {
                this.dialogVisibleAdd = true;
                this.id = this.logisticsList.length+1;
                this.ruleForm.name = '';
                this.ruleForm.pathway = '';
                this.ruleForm.address = '';
                this.ruleForm.telephone = '';
            },
            handleTop(index,row) {
                this.logisticsList.splice(index, 1)
                this.logisticsList.unshift(row)

                this.changeTop(row.id,row.top+1)
            },
            handleEdit(index,row) {
                this.dialogVisibleEdit = true;
                this.id = row.id
                this.ruleForm.name = row.name;
                this.ruleForm.pathway = row.pathway;
                this.ruleForm.address = row.address;
                this.ruleForm.telephone = row.telephone;
            },
            handleDelete(index,row) {
                this.subDelete(row)
            },
            handleClose() {
                this.dialogVisibleAdd = false;
                this.dialogVisibleEdit = false;
            },
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        let newLogistic = {
                            id: this.id,
                            name: this.ruleForm.name,
                            pathway: this.ruleForm.pathway,
                            address: this.ruleForm.address,
                            telephone: this.ruleForm.telephone
                        }

                        this.subAdd(newLogistic);
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            submitEditForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        let edited = {
                            id: this.id,
                            name: this.ruleForm.name,
                            pathway: this.ruleForm.pathway,
                            address: this.ruleForm.address,
                            telephone: this.ruleForm.telephone
                        }

                        this.subEdit(edited);
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            async getLogisticsList(page) {
                if(typeof page !== 'undefined') {
                    this.currentPage = page
                }

                let res = await this.$axios.get('/getLogistics',{params: {page:this.currentPage},headers: {Authorization:localStorage['token']}})

                this.logisticsList = res.data.data
                this.totalPages = res.data.totalPages
            },
            async subAdd(newLogistic) {
                let res = await this.$axios.post('/addLogistics',{newLogistic},{headers:{Authorization:localStorage['token']}})

                if(res.data.code === 200) {
                    this.dialogVisibleAdd = false;
                    this.dialogVisibleEdit = false;

                    this.getLogisticsList()
                }
            },
            async subEdit(edited) {
                let res = await this.$axios.post('/editLogistics',{edited},{headers:{Authorization:localStorage['token']}})

                if(res.data.code === 200) {
                    this.dialogVisibleAdd = false;
                    this.dialogVisibleEdit = false;

                    this.getLogisticsList()
                }
            },
            async subDelete(deleted) {
                let res = await this.$axios.post('/delLogistics',{id:deleted.id},{headers:{Authorization:localStorage['token']}})

                if(res.data.code === 200) {
                    this.getLogisticsList()
                }
            },
            async changeTop(id,top) {
                let res = await this.$axios.post('/changeTop',{id,top},{headers:{Authorization:localStorage['token']}})

                if(res.data.code === 200) {
                    this.getLogisticsList()
                }
            }
        },
        mounted() {
            this.getLogisticsList()
        }
    }
</script>